//
//  SearchViewController.m
//  NewBook
//
//  Created by dlios on 15-3-7.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "SearchViewController.h"

@interface SearchViewController ()

@end

@implementation SearchViewController
-(void)dealloc
{
    [_searchBar release];
    [_myTableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"搜  索";
    //搜索页面
    //搜索框
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    //searchBar.barTintColor = [UIColor redColor];
    self.searchBar.showsSearchResultsButton = YES;
    self.searchBar.showsCancelButton = YES;
    self.searchBar.showsBookmarkButton = YES;
    self.searchBar.searchResultsButtonSelected = YES;
    
    self.searchBar.placeholder = @"搜索书名";
    self.searchBar.delegate = self;
    self.searchBar.searchBarStyle = UIBarStyleBlackTranslucent;
    [self.view addSubview:self.searchBar];
    
    //创建tableview
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    //指定tableview为searchbar的搜索栏
    self.myTableView.tableHeaderView = self.searchBar;
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

}
//设置内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIndentifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifer];
    }
    
    
    return cell;
    
}


//设置tableview行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return 10;
}


//当在输入框中输入文本时触发的方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    //引用第三方
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://mapi.dangdang.com/index.php?action=all_search&user_client=iphone&client_version=5.0.1&page=1&page_size=10&sort_type=default_0&keyword=%s&img_size=h&udid=18948B21919333DDC1DDE273A67E5005&time_code=41D10183C2E651886F365F72AD222421&timestamp=1426072273&union_id=537-50&permanent_id=20150304193014100686115327268485015", searchText.UTF8String];
    
    url_string = [url_string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"0000000=%@", url_string);
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        
        NSMutableDictionary *movieDic = responseObject;
        NSLog(@"666%@", movieDic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
       }//何以笙箫默

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
